function patient = FiveD_workflow_toolbox(pt)
    
%     pause(19000)
    %| 5D model worflow code.
    
    %|  {  *WARNING: NOT FOR CLINICAL USE*   }
    
    %| Code will run the following;
    %|      pt = patient number (1 - 10 so far)
    %|      1. choose_patient(pt) selects patient's data folders;
    %|             *creates a structure 'patient' which contains all the
    %|              patients info (folder locations, static image, bellows
    %|              voltage), and is added to throughout the workflow.
    %|                     i.e. patient.dim = image dimension
    %|                          patient.ref = reference image number
    %|                          patient.scans = number of scans acquired
    %|                          patient.static = static image (reference)
    %|
    %|      2. create_model_folders_toolbox loads images and creates the
    %|                              folders required by the model code
    %|      3. read_sync_data_toolbox loads the syncronised bellows signals
    %|                              (created by helical_sync_toolbox.m)
    %|      4. calculate_volume_flow_fit_toolbox calculates flow from the
    %|                                                  bellows voltages
    %|      5. create_dvfs_slices_toolbox reads the DVFs created by
    %|          elastix, and slices them into coronal slices (each dvf.mat contains 25
    %|          coronal slices - see fn for details.
    %|      6. patient.run_scans defines which scans to analyse (default =
    %|                                                              1:25)
    %|      7. drift_correction runs the model on a single coronal slice,
    %               using fmincon to minimise the error for various linear drifts to the
    %|              bellows signals. patient.drift is caluclated, and the bellows is drift
    %|              corrected accordingly (a plot is created to check it worked ok).
    %|      8. ii,jj,kk are the voxel arrays on which to run the model
    %|                  (default = all voxels, (ie ii,jj,kk = image dimensions).
    %|      9. Modelfit_5d_parfor_toolbox runs the model (in parallel or not),
    %               solving the b= aX equation for all voxels independantly in
    %|              each direction, where;
    %|                  a = [1 voltage flow],
    %|                  b = DVFs,
    %|                  X = [constant alpha beta];  See code for details.
    %|              parameters are saved to patient.model_folder_params
    %|      10. Generate an average image from all registered scans.
    %|      11. Calculation of linear fit of HU with bellows voltage, from the registered images.
    %|      12. Calibration of the bellows voltage to lung volume, by
    %|          generating to pseudo static images, and calculating the lung voxel
    %}           volume.
    %|      13.  Generates a 'representative' breath from the sampled
    %|              breathing waveform, and deforms the reference (or average) image to 8
    %|              phases, following the Siemen's 4D-CT protocol (0% inhale, 25%in,
    %|              50%in, 75%in, 100%in, 75% exhale, 50%ex, 25%ex).
    %|      14.  Generates DICOM images from the 4D-CT (work in progress -
    %|              code to follow...)
    %------------------------------------------------------------------------
    %|      Dependancies;                                                     |
    %|                  ParforProgMon (parfor loop progress monitor).
    %|                  read_dvf_elastix_slice
    %|                  iSaveX (parfor loop save function).
    %|                  choose_patient
    %|                  create_model_folders_toolbox
    %|                  read_sync_data_toolbox
    %|                  calculate_volume_flow_fit_toolbox
    %|                  create_dvfs_slices_toolbox
    %|                  drift_correction
    %|                  Modelfit_5d_parfor_toolbox *IF PARRELEL TOOLBOX available
    %|                  Modelfit_5d_toolbox
    %------------------------------------------------------------------------
    %   This file is part of the
    %   5D-Novel4DCT Toolbox  ("Novel4DCT-Toolbox")
    %   DH Thomas, Ph.D
    %   University of California, Los Angeles
    %   Contact: mailto:dhthomas@mednet.ucla.edu
    %------------------------------------------------------------------------
    % $Author: DHThomas $	$Date: 14-Apr-2014 15-23-24 $	$Revision: 0.1 $
    close all;
    clc;
    %     uiwait(msgbox({'WARNING: Not for clinical use.', 'Continue to accept all responsibility:'}, 'WARNING'  ,'error', 'modal'));
    %     pt = 2
    choice = 6;%main_menu_toolbox;
    if nargin < 1
        prompt = 'WARNING: Not for clinical use. \n       Which Patient would you like to run? (number 1-10) ';
        pt= input(prompt);
        
        
    end
    
    patient = setup_patient_params(pt);
%         patient.run_scans = [1:8,10:12,14:25];%find(all(patient.bellows_volt));

        patient.default_figure_position = [0.1 0.1 0.81 0.81];
   
    patient.par_toolbox =  license('test', 'Distrib_Computing_Toolbox');
    if patient.par_toolbox>0;
        if matlabpool('size')== 0 % checking to see if my pool is already open
            matlabpool
        end   
    end

    if choice>=0;
        display(sprintf('Running Code for Patient %d',pt))
        %     par_toolbox = 0;
        %%
        %Slice the DVFs from Elastix into coronal slices;
        patient.dvf_folder = create_dvfs_slices_toolbox(patient);
        %%
        %choose which scans to run (eg pt = 8, run_scans = 8:25), default = all scans;
        %%
        % Solve Drift;----------------------------------
        % Check if a value for the drift exists, if not solve it and add it to patient structure;
        patient.bellows_volt(end-4:end,:) = repmat(patient.bellows_volt(end-5,:),5,1);
        patient.scan_times(end-4:end,:) = repmat(patient.scan_times(end-5,:),5,1);
        patient.scan_times(1:5,:) = repmat(patient.scan_times(5,:),5,1);
        patient.scan_times_zerod(end-4:end,:) = repmat(patient.scan_times_zerod(end-5,:),5,1);
        patient.scan_times_zerod(1:5,:) = repmat(patient.scan_times_zerod(5,:),5,1);
        
        [patient.drift, patient.bellows_volt_drifted] = drift_correction(patient);
        [~, patient.flow_drifted] = calculate_volume_flow_fit_toolbox(patient.bellows_volt_drifted, patient);

        %%
        patient.bellows_volt_drifted(:,~ismember(1:patient.scans,patient.run_scans)) = NaN;
        patient.flow_drifted(:,~ismember(1:patient.scans,patient.run_scans)) = NaN;
        
        plot_bellows_drift_correction_toolbox(patient);drawnow;
                
%         [patient.drift, patient.bellows_volt_drifted] = phase_correction(patient);

        %%
        %Solve the model for the patient, and plot results;
        
        [error_5d, patient.mean_error_5d, patient.std_error_5d] = Model_5d_wrapper_toolbox(patient);
        plot_5d_error_slice_toolbox(patient, error_5d);drawnow;
        plot_example_voxel_wrapper(patient);
        
       
%          save([patient.folder '/patient'],'patient')
    end
                 save([patient.folder '/patient'],'patient')

    if choice>=1
        %%
        %Generate average image from all registered scans;
        img_average = generate_average_scans_toolbox(patient);
        plot_average_image_toolbox(patient, img_average);
        save([patient.folder '/patient'],'patient')
        
    end
    
    if choice>=2
        %Calculate the H.U. variation with volume using the registered images;
        [hu_fit] = hu_fit_wrapper_toolbox(patient);
%         save([patient.folder '/patient'],'patient')
        
    end
    
    
    if choice>=3
        %% ------------------
        % Generate 2 psuedo phases, and calculate the lung volume:bellows voltage
        % calibration;
        
        patient.voxel_size = 1;
        [patient.volume_calibration, use_average_image] = deform_images_psuedo_phase_toolbox(patient);
        plot_calibrated_bellows_toolbox(patient)
%         save([patient.folder '/patient'],'patient')
        
        %     end
    end
    
    if choice>=4
        %% Generate 4D-CT (.mat files only);
        patient.use_average_image = 1;
        Generate_4DCT_toolbox(patient, patient.use_average_image);%0);
        save([patient.folder '/patient'],'patient')
     
        
    end
    
    if choice>5
        %% Save 4D-CT scans as DICOM;
%         load([patient.folder '/patient'])
       patient = save_4DCT_as_DICOM_toolbox(patient)
             
    end
     if choice>6
%         load([patient.folder '/patient'])
        deform_images_psuedo_originalscans_toolbox(patient)
        save([patient.folder '/patient'],'patient')
             
     end
     
     if choice>7
        %% Save Psuedo scans as DICOM;
%         load([patient.folder '/patient'])
        save_PsuedoScan_as_DICOM_toolbox(patient)
             
    end
    
end



